package org.btik.light.server.dev.manage;

import org.btik.light.meta.exception.LightException;
import org.btik.light.server.platform.common.api.bean.dev.access.DevAccountDto;
import org.btik.light.server.platform.common.api.bean.dev.access.DevAccountVo;
import org.btik.light.server.platform.common.api.service.DevAccountService;
import org.btik.light.server.platform.common.api.dao.LightDao;
import org.btik.light.server.platform.common.api.bean.dev.access.DevAccount;
import org.btik.light.tool.MDUtil;
import org.btik.light.tool.type.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

import static java.util.stream.Collectors.toMap;

/**
 * @author lustre
 * @since 2023/6/3 15:40
 */
@Service
public class DevAccountServiceImpl implements DevAccountService {

    @Autowired
    private LightDao lightDao;

    @Override
    public Collection<DevAccountVo> list() {
        return lightDao.list(DevAccount.class).stream()
                .map(devAccount -> new DevAccountVo(devAccount.getUsername(),
                        devAccount.getMdAlgorithm(), devAccount.getStatus()))
                .toList();
    }

    @Override
    public Map<String, String> getMdAlgorithms() {
        return Arrays.stream(MDUtil.Algorithm.values()).collect(
                toMap(MDUtil.Algorithm::name, MDUtil.Algorithm::getAlgorithmName,
                        (oldValue, newValue) -> newValue, LinkedHashMap::new));
    }

    @Override
    public boolean saveAccount(DevAccountDto devAccountDto) {
        String username = devAccountDto.getUsername();
        if (StringUtil.isEmpty(username)) {
            return false;
        }
        DevAccount devAccount = lightDao.get(username, DevAccount.class);
        if (devAccount == null) {
            devAccount = new DevAccount();
            devAccount.setUsername(username);
        }
        if (devAccountDto.getStatus() != null) {
            devAccount.setStatus(devAccountDto.getStatus());
        }
        if (devAccountDto.getMdAlgorithm() != null) {
            devAccount.setMdAlgorithm(devAccountDto.getMdAlgorithm());
        }
        if (!StringUtil.isEmpty(devAccountDto.getPassword())) {
            devAccount.setPasswd(MDUtil.digestAsHex(
                    devAccount.getMdAlgorithm(), username + devAccountDto.getPassword()));
        }
        lightDao.save(devAccount);
        return true;
    }

    @Override
    public boolean delete(String username) {
        if (StringUtil.isEmpty(username)) {
            return false;
        }
        DevAccount devAccount = lightDao.get(username, DevAccount.class);
        if (devAccount == null) {
            return false;
        }

        lightDao.delete(username, DevAccount.class);
        return true;
    }

    @Override
    public DevAccountVo getUserByName(String username) {
        if (StringUtil.isEmpty(username)) {
            throw new LightException("username can not be empty");
        }
        DevAccount devAccount = lightDao.get(username, DevAccount.class);
        if (devAccount == null) {
            return null;
        }
        return new DevAccountVo(devAccount.getUsername(), devAccount.getMdAlgorithm(), devAccount.getStatus());
    }

    @Override
    public boolean changeStatus(String username, Boolean status) {
        if (StringUtil.isEmpty(username)) {
            throw new LightException("username can not be empty");
        }
        if (status == null) {
            throw new LightException("status can not be empty");
        }
        DevAccount devAccount = lightDao.get(username, DevAccount.class);
        if (devAccount == null) {
            return false;
        }
        devAccount.setStatus(status);
        lightDao.save(devAccount);
        return true;
    }
}
